import java.util.Vector;

/**
 * Class holding the structure of an Intersection.
 * @author Jared Lindbl�m
 */
public class Intersection {
	private String name;
	private Vector<Road> roads;
	private Vector<Section> roadSections;
	
	/**
	 * Constructor.
	 * @param name	The name of the Intersection.
	 */
	public Intersection (String name) {
		this.name = name;
		roads = new Vector<Road>();
		roadSections = new Vector<Section>();
	}

	/**
	 * Accessor Method (Name).
	 * @return	The name of the Intersection.
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Accessor Method (Road Sections)
	 * @param currentRoadSection The current section of road the vehicle is on.
	 * @return	An array of road sections excluding the current road section.
	 */
	public Section[] getRoadSections(Section currentRoadSection) {
		Section[] r;
		
		if (currentRoadSection != null) {
		@SuppressWarnings("unchecked")
		Vector<Section> roadSectionRemoved = (Vector<Section>) roadSections.clone();
		
		roadSectionRemoved.removeElement(currentRoadSection);
		r = new Section[roadSectionRemoved.size()];
		
		roadSectionRemoved.toArray(r);
		}
		else {
			r = new Section[roadSections.size()];
			roadSections.toArray(r);
		}
		
		return r;
	}

	/**
	 * Associates a road with the intersection.
	 * @param road	The road to be associated to this intersection.
	 */
	public void addRoad(Road road) {
		roads.addElement(road);
	}
	
	/**
	 * Associates a road section with the intersection.
	 * @param roadSection	The road section to be associated to this intersection.
	 */
	public void addRoadSection (Section roadSection) {
		roadSections.addElement(roadSection);
	}

	/** 
	 * Prints the structure of this intersection (for debugging).
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String print = "Name: " + name + "\n";

		Road[] roadArray = new Road[roads.size()];
		
		roads.toArray(roadArray);
		
		print = print.concat("\n\t\t\tAssociated Roads: (" + roadArray.length + " in total)\n\n");
		for (int i = 0; i < roadArray.length; i++) {
			print = print.concat("\t\t\t\tName: " + roadArray[i].getName() + "\n");
		}
		
		Section[] sectionArray = new Section[roadSections.size()];
		
		roadSections.toArray(sectionArray);
		
		print = print.concat("\n\t\t\tAssociated Sections: (" + sectionArray.length + " in total)\n\n");
		for (int i = 0; i < sectionArray.length; i++) {
			print = print.concat("\t\t\t\tBetween: " + sectionArray[i].getDescription() + "\n");
		}
		
		return print;
	}
	
}